/**
 * Project Name:javaBaseReview
 * Package Name:com.xuwei.aopframework
 * author:David
 * Date:2015年6月7日上午10:55:41
 * Copyright (c) 2015, xw123box@126.com All Rights Reserved.
 *
 */
package com.xuwei.ext.aopframework;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import com.xuwei.ext.proxy.Advice;

/**
 * @author David since 2015年6月7日 上午10:55:41
 * @version
 * @since JDK 1.6
 */
public class BeanFactory {

	Properties props = new Properties();

	public BeanFactory(InputStream ips) {
		try {
			props.load(ips);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public Object getBean(String name){
		String className=props.getProperty(name);
		Object bean=null;
		try {
			Class clazz=Class.forName(className);
			bean=clazz.newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		} 
		if(bean instanceof ProxyFactoryBean){
			Object proxy=null;
			ProxyFactoryBean proxyFactoryBean=(ProxyFactoryBean)bean;
			Advice advice;
			Object target;
			try {
				advice = (Advice)Class.forName(props.getProperty(name+".advice")).newInstance();
				target = Class.forName(props.getProperty(name+".target")).newInstance();
				proxyFactoryBean.setAdvice(advice);
				proxyFactoryBean.setTarget(target);
			} catch (Exception e) {
				e.printStackTrace();
			}
 			proxy = ((ProxyFactoryBean)bean).getProxy();
			return proxy;
		}
		return bean;
	}
}
